// void, Obj This

Settlement this;
ObjList olUnitsTributeOut, olShip; 
Unit u;
Ship shipu;
int i, x, j, p, threshold, r;
str class_unit;
  
Sleep(5040+rand(GetConst("RandTimeStart"))+GetConst("AddTimeStart"));

this = This.AsBuilding().settlement;
//pr("shipyard idle board dziala");
r = EnvReadInt(.player, "RACE");

if (r==Carthage){
       class_unit = "ShipTransportCarthage";
} else if (r==Iberia){
       class_unit = "ShipTransportIberia";
} else if (r==Britain){
       class_unit = "ShipTransportBritain";
} else if (r==Germany){
       class_unit = "ShipTransportGermany";
} else if (r==Egypt){
       class_unit = "ShipTransportEgypt";
} else if (r==Gaul){
       class_unit = "ShipTransportGaul";
} else if(r == ImperialRome || r == RepublicanRome){
       class_unit = "ShipTransportRome"; 
} else {
  	class_unit = "ShipTransportGaul";
}

p = .player;
        
while (p == .player){
	
	Sleep(2000);
	while(.Units.count >= 1){
		Hero hero;
		Unit y;
		bool allinholder = true;
		ObjList ol;
		
		ol.Clear;
	
		ol = .Units.ObjClass("Unit");
		for(i=0; i<ol.count; i+=1){
			
			Sleep(2);
			u = ol[i].AsUnit;
			if(u.IsValid){
				if(u.IsHeirOf("Hero")){
					if(u.AsHero.HasArmy){
						for(x=0; x<u.AsHero.army.count; x+=1){
							y = u.AsHero.army[x].AsUnit;
							if(y.IsValid){
								Sleep(5);
								if(!y.InHolder || y.command != "idle")
									allinholder = false;
									break;
							}
						}
					}
				} else if(u.hero.IsValid){
					//Hero h;
					if(hero.IsValid)
						if(u.hero.AsHero == hero) continue;
					hero = u.hero.AsHero;
					if(hero.IsValid){
						for(x=0; x<hero.army.count; x+=1){
							y = hero.army[x].AsUnit;
							if(y.IsValid){
								Sleep(5);
								if(!y.InHolder || y.command != "idle")
									allinholder = false;
									break;
							}
						}
					}
				}
			}
		}

		Sleep(2000);
		if(allinholder) break;
	}
	
	if(.Units.count >= 1){
		ObjList olInShip;
		
		olUnitsTributeOut.Clear;
		threshold = 10; // prog - od kiedy zacznie przewozic (30 ludzi)
		olShip.Clear();
		for(i=0; i<.Units.count; i+=1){
			Sleep(5);
			if(!.Units[i].IsValid) continue;
			if( (.Units[i].IsMilitary && 
				!.Units[i].IsHeirOf("ShipBattle") &&
				!.Units[i].IsHeirOf("ShipTransport")) || .Units[i].IsHeirOf("Wagon")) {
				if(.Units[i].IsValid){
					if(.Units[i].IsHeirOf("Wagon")) threshold = 1;				 
					olUnitsTributeOut.Add(.Units[i]);
				}
			} else if(.Units[i].IsHeirOf("ShipBattle") ||
					  .Units[i].IsHeirOf("ShipTransport") ){
				if(.Units[i].IsValid)
					olShip.Add(.Units[i]);
			}
		}
		if(olShip.count >= 1){
			for(i=0; i<olShip.count; i+=1){
				shipu = olShip[i].AsShip;
				if(!shipu.IsValid) continue;
				if(shipu.UnitsCount >= 1){
					
					olInShip = shipu.GetUnitsOnBoard;
					shipu.UnboardUnits(olInShip);
					for(j=0; j<olInShip.count; j+=1){
						u = olInShip[j].AsUnit; 
						if(!u.IsValid) continue;
						if(u.IsHeirOf("Hero")){
							.ForceAddUnit(u);
							olInShip.Remove(u);
						}
						Sleep(5);
					}
					
					for(j=0; j<olInShip.count; j+=1){
						u = olInShip[j].AsUnit; 
						if(!u.IsValid) continue;
						.ForceAddUnit(u);
						Sleep(5);
					} 
				}
				if(shipu.IsHeirOf("ShipTransport"))
					shipu.Damage(10000);	
			}
		}
			
		if(EnvReadString(this, "tribute_people") == "on"){
			if( olUnitsTributeOut.count >= 1 ){
				Settlement sett_target;
				str target_name;
			
				target_name = EnvReadString(this, "tribute_people_target");
				sett_target = GetNamedObj(target_name).obj.AsBuilding.settlement;                        
				
				if(sett_target.IsValid){
					Building bld_target;
					
					bld_target = sett_target.GetCentralBuilding();
					if(bld_target.IsHeirOf("BaseShipyard") && sett_target.WaterLsa == .WaterLsa){ 
						if( olUnitsTributeOut.count >= threshold ){ 
							Ship ship;                                        
							
							Sleep(50);
							ship = .CreateShip(class_unit);
							if(ship.IsValid){
								ObjList ol, olHero, olUhero;
								int max_count;	                                                

								ol.Clear;
								olHero.Clear;
								olUhero.Clear;
									
								if(olUnitsTributeOut.count > ship.AsShip.UnitsMax)
									max_count = ship.AsShip.UnitsMax;
								else
									max_count = olUnitsTributeOut.count;
									
								for(i=0; i<max_count; i+=1){
									u = olUnitsTributeOut[i].AsUnit;
									if(u.IsValid){
										bool hasarmy = false;
										if(u.IsHeirOf("Hero"))
											if(u.AsHero.HasArmy)
												hasarmy = true;
										
										if(u.command == "idle" && u.InHolder){
											if(hasarmy){
												olHero.Add(u);
											} else {
												ol.Add(u);
											}
										}
									}
									Sleep(5);
								}
								Sleep(50);
								if(olHero.count >= 1){
									Hero h;
										
									h = olHero[0].AsHero;
									for(i=0; i<olUnitsTributeOut.count; i+=1){
										u = olUnitsTributeOut[i].AsUnit;
										if(u.IsValid){
											if(u.command == "idle" && u.InHolder){
												if(!u.IsHeirOf("Hero")){
													if(u.hero.IsValid){
														if(u.hero.AsHero == h)
															olUhero.Add(u);	
													}
												}
											}
										}
										Sleep(5);
									}
									Sleep(50);
									if(olUhero.count == h.army.count){
										for(i=0; i<h.army.count; i+=1){
											u = h.army[i].AsUnit;
											if(u.IsValid){
												if(u.command == "idle" && u.InHolder){
													Sleep(5);
													ship.BoardUnit(u);
													
												}
											}
										}
										if(h.command == "idle" && h.InHolder)
											ship.BoardUnit(h); 
									} else {
										ship.Damage(10000);
										continue;
									}
								} else {
									if(ol.count >= 1){
										Sleep(50);
										for(i=0; i<ol.count; i+=1){
											u = ol[i].AsUnit;
											if(u.IsValid){
												if(u.command == "idle" && u.InHolder){
													Sleep(5);
													ship.BoardUnit(u);
												}
											}
										}
									}
								}
								
								if(ship.UnitsCount >= 1){
									Sleep(5);
									ship.SetCommand("enter", bld_target);
									Sleep(3000);
								} else {
									ship.Damage(10000);
								}
							}
						}
					}
				} 
				//if(IsPointInWater(pp))
			}
		}
    }        
}